package com.example.jbmsuppliers.sinopec.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.net.NetAddRess;
import com.example.jbmsuppliers.sinopec.orderaddress.MyOrderAll;
import com.example.jbmsuppliers.sinopec.orderaddress.MyOrderDqr;
import com.example.jbmsuppliers.sinopec.orderaddress.MyOrderEvalua;
import com.example.jbmsuppliers.sinopec.view.DoubleButtonDialog;
import com.example.jbmsuppliers.sinopec.view.MyToast;
import com.jbm.jbmsupplier.util.IOrder;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by 家帮忙 on 2016/9/30.
 */

public class MyListAdapter extends BaseAdapter {
    Context c;
    ArrayList<HashMap<String,Object>> arrayList;
    private final int ORDERSTATIC = 0003;
    private final int EXIT = 0004;
    private String biaoshi;
    public MyListAdapter(Context c, ArrayList<HashMap<String, Object>> arrayList,String str) {
        this.c = c;
        this.arrayList = arrayList;
        this.biaoshi=str;
    }
    public void setNotify(ArrayList<HashMap<String,Object>> arrayLists){
        arrayList=(ArrayList<HashMap<String,Object>>)arrayLists;
        notifyDataSetChanged();

    }
    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return arrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        if (view==null){
            view=View.inflate(c, R.layout.orderrlr_recyle_layout,null);
        }
        final TextView te_or_bianhao,te_or_zhaugntai,te_address_qian,te_order_neirong,te_order_shijian,te_or_sta,te_or_photo,daitipin;
        te_or_sta= (TextView) view.findViewById(R.id.te_or_sta);
        te_or_bianhao= (TextView) view.findViewById(R.id.te_or_bianhao);
        te_address_qian= (TextView) view.findViewById(R.id.te_address_qian);
        te_order_neirong= (TextView) view.findViewById(R.id.te_order_neirong);//地址
        te_order_shijian= (TextView) view.findViewById(R.id.te_order_shijian);//姓名
        te_or_zhaugntai= (TextView) view.findViewById(R.id.te_or_zhaugntai);
         te_or_photo = (TextView) view.findViewById(R.id.te_or_photo);//手机号
        daitipin = (TextView) view.findViewById(R.id.daitipin);//手机号
        if (arrayList.get(position).get("status").equals("new")){
            te_or_sta.setText("取消");
            te_or_sta.setVisibility(View.VISIBLE);
            te_or_zhaugntai.setText("订单"+"待确认");}
        if (arrayList.get(position).get("status").equals("temp")){
            te_or_sta.setText("取消");
            te_or_sta.setVisibility(View.VISIBLE);
            te_or_zhaugntai.setText("订单"+"待确认");}
        if (arrayList.get(position).get("status").equals("confirm")){
            te_or_sta.setVisibility(View.GONE);
            te_or_zhaugntai.setText("订单"+"进行中");}
        if (arrayList.get(position).get("status").equals("completed")){
            te_or_sta.setText("评价");
            te_or_sta.setVisibility(View.VISIBLE);
            te_or_zhaugntai.setText("订单"+"待评价");}
        if (arrayList.get(position).get("status").equals("revoke")){
            te_or_sta.setVisibility(View.GONE);

            te_or_zhaugntai.setText("订单"+"已作废");}
        if (arrayList.get(position).get("status").equals("comment")){
            te_or_sta.setVisibility(View.GONE);

            te_or_zhaugntai.setText("订单"+"已评价");}
        te_address_qian.setText(arrayList.get(position).get("amount")+"");//价格
        te_or_bianhao.setText(arrayList.get(position).get("order_id")+"");//编号
        te_or_photo.setText(arrayList.get(position).get("create_date")+"");//手机号
        te_order_shijian.setText(arrayList.get(position).get("item_desc")+"");//姓名
        te_order_neirong.setText(arrayList.get(position).get("name")+"");//地址
        te_or_sta.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {




                if(te_or_sta.getText().toString().equals("取消")){

                    final DoubleButtonDialog dialogLoading=new DoubleButtonDialog(c,"确定要取消订单");
                    dialogLoading.getokbtn().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            NetAddRess netAddRess = new NetAddRess(handler);
                            netAddRess.getOrderCancel(arrayList.get(position).get("order_id")+"",arrayList.get(position).get("status")+"", ORDERSTATIC);
                            dialogLoading.dismiss();
                        }
                    });

                    dialogLoading.getnobtn().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialogLoading.dismiss();
                        }
                    });

                }else if (te_or_sta.getText().toString().equals("评价")){

                   c. startActivity(new Intent(c, MyOrderEvalua.class).putExtra("id",arrayList.get(position).get("order_id")+""));


                }else if(arrayList.get(position).get("status").equals("confirm")){
                    MyToast.show(c,"订单正在作业中不能取消",0);
        }

            }
        });
        return view;








    }

  public   interface setDate{
        void setdatedqr();
    }



Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
        case ORDERSTATIC:{
            final IOrder revokeorder = (IOrder) msg.obj;
            if (revokeorder.back) {
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                MyToast.show(c,"亲！订单取消成功",0);
if (biaoshi.equals("all")){
    ((MyOrderAll) c).setdatedqr();
}else if (biaoshi.equals("dqr")){
    ((MyOrderDqr) c).setdatedqr();
}
                            }
                        }, 700);

            } else {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                                MyToast.show(c,revokeorder.errorinfo,0);
                            }
                        }, 700);



            }
            break;
        }


        }
        super.handleMessage(msg);
    }
};



}
